package com.tensquare.sms.customer;

import com.aliyuncs.CommonResponse;
import com.tensquare.sms.SmsUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author:zimmor
 * @Date: 2021/3/11 15:50
 * @Description
 * @version: 1.0.0
 */
@Component
@RabbitListener(queues = "message")
public class SmsCustomer {
    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${aliyun.sms.sign_name}")
    private String signName;

    @Value("${aliyun.sms.template_code}")
    private String templateCode;

    @Autowired
    private SmsUtil smsUtil;

    @RabbitHandler
    public void getMessage(Map<String, String> map) {
        String mobile = map.get("mobile");
        String code = map.get("code");
        String param = "{\"code\":"+code+"}";
        System.out.println("电话号码是："+mobile);
        System.out.println("验证码是："+code);
        //调用阿里的短信服务发送短信
        CommonResponse commonResponse = smsUtil.sendMessage(mobile, signName, templateCode, param);
        System.out.println(commonResponse.getData().toString());
        //成功发送后，把验证码存入redis
        redisTemplate.opsForValue().set("smsCode_"+mobile, code, 2, TimeUnit.MINUTES);
    }
}
